#ifndef GLGRAPHICS_H
#define GLGRAPHICS_H

#include "Graphics.h"
#include "Application.h"

#include <vector>
#include <algorithm>

#define MAX_QUADS 16384

#ifdef _DEBUG
#define GL_ERR(func) \
{ \
   func; \
   if(glGetError() != GL_NO_ERROR) \
   { \
		criticalError(#func " failed"); \
   } \
}
#else
#define GL_ERR(func) func
#endif

namespace GenBen {

class GLTexture : public Texture
{
public:
	GLTexture(const string& filename);
	virtual ~GLTexture();

	virtual uint getWidth() { return width; };
	virtual uint getHeight() { return height; };

	virtual void release() {}; // HACK

	uint getTexId() { return tex_id; };

private:
	uint tex_id;
	uint width, height;
};

struct GLRect
{
	RectI source, dest;
	Color tint;
	GLTexture* tex;
	int layer;

	bool operator<(const GLRect& rect) const
	{
		if(layer != rect.layer)
			return layer < rect.layer;
		return tex->getTexId() < rect.tex->getTexId(); 
	}
};

class GLGraphics : public Graphics
{
public:
	GLGraphics(uint width, uint height, uint scale, uint refresh_rate, bool fullscreen, bool lcd);
	virtual ~GLGraphics();

	virtual Texture* loadTexture(string filename);

	virtual string getDescription();
	
	virtual void clear(const Color& color);
	virtual void present();

	virtual void draw(Texture* texture, int layer, const RectI& source, const VectorI& dest, const Color& tint); 
	virtual void draw(Texture* texture, int layer, const RectI& source, const RectI& dest, const Color& tint); 
	virtual void draw(Texture* texture, int layer, const RectI& source, const VectorI& dest); 
	virtual void draw(Texture* texture, int layer, const RectI& source, const RectI& dest);

private:
	uint rect_count;
	std::vector<GLRect> rects;

};

}

#endif
